﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionAlarmas.BusinessEntities;
using System.Data.Linq;
using GestionAlarmas.MessageContracts;
using GestionAlarmas.DataContracts;

namespace GestionAlarmas.ServiceImplementation
{
    public partial class GestionAlarmasService : GestionAlarmasServiceBase
    {

        public override GestionAlarmas.MessageContracts.ListaResponse ListaAlarmasUsuario(GestionAlarmas.MessageContracts.ListaRequest request)
        {
            ListaResponse response = new ListaResponse();
            response.Alertas = new GestionAlarmas.DataContracts.ListaAlertas();
            using (AlarmasDataClassesDataContext adc = new AlarmasDataClassesDataContext())
            {
                var alarmas = (from a in adc.ALERTAs
                               where a.USUARIO_ID.Equals(request.IdUsuario)
                               select a).ToArray();
                foreach (var alarma in alarmas)
                {
                    Alerta alerta = new Alerta();

                    alerta.Hora = alarma.ALERTA_HORA;
                    alerta.IdAlerta = alarma.ALERTA_ID;
                    alerta.IdLinea = alarma.LINEA_ID;
                    alerta.IdParada = alarma.PARADA_ID;
                    alerta.IdUsuario = alarma.USUARIO_ID;
                    alerta.Minutos = alarma.ALERTA_MINUTOS;

                    // D{as a mostrar
                    object[][] dias = new object[2][];
                    dias[0] = new object[] {alarma.ALERTA_DIAS.ToCharArray()};
                    dias[1] = new object[] { 0, 1, 2, 3, 4, 5, 6 };
                    alerta.Dias = (from d in dias[1] where ((char[])(dias[0][0]))[(int)d].Equals('1') select ((DayOfWeek)d).ToString()).ToArray();
                    response.Alertas.Add(alerta);
                }
            }
            return response;
        }

        public override AltaResponse AltaAlerta(AltaRequest request)
        {
            AltaResponse response = new AltaResponse();
            response.AltaExitosa = false;
            try
            {
                using (AlarmasDataClassesDataContext adc = new AlarmasDataClassesDataContext())
                {
                    ALERTA alerta = new ALERTA();
                    alerta.ALERTA_DIAS = request.Alarma.Dias;
                    alerta.ALERTA_HORA = request.Alarma.Hora;
                    alerta.ALERTA_MINUTOS = request.Alarma.Minutos;
                    alerta.LINEA_ID = request.Alarma.IdLinea;
                    alerta.PARADA_ID = request.Alarma.IdParada;
                    alerta.USUARIO_ID = request.Alarma.IdUsuario;

                    adc.ALERTAs.InsertOnSubmit(alerta);
                    adc.SubmitChanges();
                    response.AltaExitosa = true;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return response;
        }

        public override ObtenerAlertaResponse ObtenerAlerta(ObtenerAlertaRequest request)
        {
            ObtenerAlertaResponse response = new ObtenerAlertaResponse();

            try
            {
                using (AlarmasDataClassesDataContext adc = new AlarmasDataClassesDataContext())
                {
                    var alertas = (from a in adc.ALERTAs
                                   where a.ALERTA_ID == request.IdAlerta
                                   select a).ToArray();
                    if (alertas.Length == 0)
                    {
                        response.Alerta = null;
                    }
                    else
                    {
                        var alerta = alertas.First();
                        response.Alerta = new Alerta();
                        response.Alerta.Dias = new string[] { alerta.ALERTA_DIAS };
                        response.Alerta.Hora = alerta.ALERTA_HORA;
                        response.Alerta.IdAlerta = alerta.ALERTA_ID;
                        response.Alerta.IdLinea = alerta.LINEA_ID;
                        response.Alerta.IdParada = alerta.PARADA_ID;
                        response.Alerta.IdUsuario = alerta.USUARIO_ID;
                        response.Alerta.Minutos = alerta.ALERTA_MINUTOS;
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return response;
        }

        public override ModificarAlertaResponse ModificarAlerta(ModificarAlertaRequest request)
        {
            ModificarAlertaResponse response = new ModificarAlertaResponse();
            response.AlertaModificada = false;
            try
            {
                using (AlarmasDataClassesDataContext adc = new AlarmasDataClassesDataContext())
                {
                    var alertas = (from a in adc.ALERTAs
                                   where a.ALERTA_ID == request.Alerta.IdAlerta
                                   select a).ToArray();

                    if (alertas.Length == 1)
                    {
                        var alerta = alertas.First();
                        alerta.ALERTA_DIAS = request.Alerta.Dias[0];
                        alerta.ALERTA_HORA = request.Alerta.Hora;
                        alerta.ALERTA_MINUTOS = request.Alerta.Minutos;
                        alerta.LINEA_ID = request.Alerta.IdLinea;
                        alerta.PARADA_ID = request.Alerta.IdParada;
                        alerta.USUARIO_ID = request.Alerta.IdUsuario;

                        adc.SubmitChanges();
                        response.AlertaModificada = true; 
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return response;
        }

        public override QuitarAlertaResponse QuitarAlerta(QuitarAlertaRequest request)
        {
            QuitarAlertaResponse response = new QuitarAlertaResponse();
            response.AlertaBorrada = false;

            using (AlarmasDataClassesDataContext adc = new AlarmasDataClassesDataContext())
            {
                try
                {
                    adc.ExecuteCommand("DELETE FROM ALERTA WHERE ALERTA_ID = " + request.IdAlerta.ToString(), new object[] { });
                    response.AlertaBorrada = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }


            return response;
        }

    }
}
